//
//  ViewController.swift
//  UISwitchDemo
//
//  Created by itshizhan on 2020/11/2.
//  Copyright © 2020 itshizhan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {
	
	
	lazy var swith:UISwitch = {
		let swith = UISwitch()
		// 注意：虽然设置了width 和 height，并不能改变大小
		swith.frame = CGRect(x: 20, y: 64, width: 100, height: 40)
		swith.tintColor = UIColor.blue
		//开关打开时背景颜色
		swith.onTintColor = UIColor.green
		//小圆圈的颜色
		swith.thumbTintColor = UIColor.orange
		swith.addTarget(self,action:#selector(switchChanged(_:)), for: .valueChanged)
		//设置默认值
		swith.isOn = true
		return swith
	}()
	
	lazy var lable:UILabel = {
		let lable = UILabel()
		// 注意：虽然设置了width 和 height，并不能改变大小
		lable.frame = CGRect(x: 20, y: 124, width: 160, height: 40)
		return lable
	}()
	
	override func viewDidLoad() {
		super.viewDidLoad()
		view.addSubview(swith)
		view.addSubview(lable)
		let state = swith.isOn ? "On" : "Off"
		lable.text = "当前状态：" + state
		//监听swift状态改变
		
	}
	
	@objc func switchChanged(_ sender: UISwitch){
		print(sender.isOn)
		lable.text = "当前状态：\(sender.isOn ? "On" : "Off") "
	}
	
	
}

